using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player : PhysicsObject
{

    private static player instance;
    public AudioSource audio;
    public AudioClip get,click,hurt;
    // Start is called before the first frame update
   public void GetAudio()
   {
    audio.clip=get;
    audio.Play();
   }
    public void ClickAudio()
   {
    audio.clip=click;
    audio.Play();
   }

 public void HurtAudio()
   {
    audio.clip=hurt;
    audio.Play();
   }
    

    public float maxSpeed = 7;
    public float jumpTakeOffSpeed = 7;
    private SpriteRenderer spriteRenderer;
    //public LayerMask ground;
     private int count;
    PlayerState playersetart;
   
    enum PlayerState
    {
        one,
        two,
        three,
        four,
        five

    }

    // private Animator animator;

    // Use this for initialization
    void Awake()
    {
         playersetart=PlayerState.one;
        spriteRenderer = GetComponent<SpriteRenderer>();
        //animator = GetComponent<Animator> ();
         instance=this;

    }

    protected override void ComputeVelocity()
    {
        Vector2 move = Vector2.zero;

        move.x = Input.GetAxis("Horizontal");
        if (Input.GetKey(KeyCode.W) && grounded)
        {
            velocity.y = jumpTakeOffSpeed;
        }
        else if (Input.GetKey(KeyCode.W))
        {
            if (velocity.y > 0)
            {
                velocity.y = velocity.y * 1f;
            }
        }


        if (move.x > 0.01f)
        {
            if (spriteRenderer.flipX == true)
            {
                spriteRenderer.flipX = false;
            }
        }
        else if (move.x < -0.01f)
        {
            if (spriteRenderer.flipX == false)
            {
                spriteRenderer.flipX = true;
            }
        }

        //animator.SetBool ("grounded", grounded);
        //animator.SetFloat ("velocityX", Mathf.Abs (velocity.x) / maxSpeed);

        targetVelocity = move * maxSpeed;
    }

    private void OnTriggerEnter2D(Collider2D collision) 
   {
    
   
    if(collision.tag=="Collection")
    {
        if(playersetart==PlayerState.one)
        {
            //collision.gameObject.SetActive(false);
            Destroy(collision.gameObject); 
           GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("迭3");
           Debug.Log("ashdkj");
           playersetart=PlayerState.two;
           player.instance.GetAudio();
        }
    }


       if(collision.tag=="Collection1")
       {
         if(playersetart==PlayerState.two)
        {
            collision.gameObject.SetActive(false);
           GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("迭4");
           playersetart=PlayerState.three;
           player.instance.GetAudio();
        }
       }

       if(collision.tag=="Collection2")
    {
    
        if(playersetart==PlayerState.three)
        {
           collision.gameObject.SetActive(false);
           GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("迭5");
           playersetart=PlayerState.four;
        
           player.instance.GetAudio();
        }
    }

    if(collision.tag=="Collection3")
    {

         if(playersetart==PlayerState.four)
        {
           collision.gameObject.SetActive(false);
           GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("迭-小篆");
           playersetart=PlayerState.five;
           player.instance.GetAudio();
        }
       
        
    }
   }

}
  


